/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bddfirstproject.view;

import bddfirstproject.MainController;
import bddfirstproject.controller.AuteurDAL;
import bddfirstproject.controller.PartieRedactionDAL;
import bddfirstproject.controller.ThemeDAL;
import bddfirstproject.exception.OracleExceptionManager;
import bddfirstproject.model.Auteur;
import bddfirstproject.model.PartieRedaction;
import bddfirstproject.model.Theme;
import java.sql.SQLException;

/**
 *
 * @author epulapp
 */
public class EditionPartieArticleView extends NamedJPanel {
    
    protected static final String title = "Edition d'une partie d'article";
    
    public static final int STATE_EDIT_DEFAULT = 0;
    public static final int STATE_EDIT_ADMIN = 1;
    public static final int STATE_EDIT_EDITOR = 2;
    public static final int STATE_CREATE_DEFAULT = 3;
    protected int viewState;
    
   
    //////////////////////////////////////////
    // Constructeurs avec gestion des etats //
    //////////////////////////////////////////
    
    public EditionPartieArticleView() {
        initComponents();
        
        // Recuperation de la vue principale
        MainController mainController = MainController.getInstance();
        
        // Gestion du status de la vue en fonction des données courantes
        Auteur currentAuteur = mainController.getCurrentAuteur();
        Theme currentTheme = mainController.getCurrentTheme();
        PartieRedaction currentPartie = mainController.getCurrentPartieEdition();
        
        if(currentTheme.getIdChefRedactionTheme() == currentAuteur.getIdAuteur()) {
            this.viewState = STATE_EDIT_ADMIN;
        } else if(currentPartie.getIdAuteurPartie() == currentAuteur.getIdAuteur()) {
            this.viewState = STATE_EDIT_EDITOR;
        } else {
            this.viewState = STATE_EDIT_DEFAULT;
        }
        
        // Partie des objets courants
        PartieRedaction partieEdition = mainController.getCurrentPartieEdition();
        Theme themePartieEdition   = ThemeDAL.getTheme(partieEdition.getIdThemePartie());
        Auteur auteurPartieEdition = AuteurDAL.getAuteur(partieEdition.getIdAuteurPartie());
        Auteur chefRedac = AuteurDAL.getAuteur(themePartieEdition.getIdChefRedactionTheme());
        
        // Insertion des données
        labelAuteur.setText(labelAuteur.getText() + auteurPartieEdition.getNomAuteur());
        labelChefRedac.setText(labelChefRedac.getText() + chefRedac.getNomAuteur());
        labelContact.setText(labelContact.getText() + chefRedac.getMailAuteur());
        labelTheme.setText(labelTheme.getText() + themePartieEdition.getTitreTheme());
        labelInclusion.setText(labelInclusion.getText() + mainController.getCurrentArticle().getTitrePartie());
        labelTitre.setText(partieEdition.getTitrePartie());
        textFieldContenu.setText(partieEdition.getContenuPartie());
        
        this.manageVisibility();
    }
    
    protected void manageVisibility() {
        this.labelErrorContent.setVisible(false);
        this.labelValidContent.setVisible(false);
        
        switch(this.viewState) {
            case STATE_CREATE_DEFAULT:
                this.btnSaveArticle.setVisible(false);
                this.btnArchiverArticle.setVisible(false);
                this.textFieldContenu.setEnabled(false);
                break;
            case STATE_EDIT_EDITOR:
                this.btnSaveArticle.setVisible(true);
                this.btnArchiverArticle.setVisible(false);
                this.textFieldContenu.setEnabled(true);
                break;
            case STATE_EDIT_ADMIN:
                this.btnSaveArticle.setVisible(true);
                this.btnArchiverArticle.setVisible(true);
                this.textFieldContenu.setEnabled(true);
                break;
            default:
                this.btnSaveArticle.setVisible(false);
                this.btnArchiverArticle.setVisible(false);
                this.textFieldContenu.setEnabled(false);
                break;
        }
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        labelTitreEcran = new javax.swing.JLabel();
        labelAuteur = new javax.swing.JLabel();
        labelTheme = new javax.swing.JLabel();
        labelInclusion = new javax.swing.JLabel();
        labelTitre = new javax.swing.JLabel();
        btnRetourListeTree = new javax.swing.JButton();
        btnArchiverArticle = new javax.swing.JButton();
        labelContact = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        textFieldContenu = new javax.swing.JTextArea();
        labelErrorContent = new javax.swing.JLabel();
        labelValidContent = new javax.swing.JLabel();
        btnSaveArticle = new javax.swing.JButton();
        labelChefRedac = new javax.swing.JLabel();

        labelTitreEcran.setFont(new java.awt.Font("Tahoma", 0, 16)); // NOI18N
        labelTitreEcran.setText("Edition d'une partie");

        labelAuteur.setText("Auteur : ");

        labelTheme.setText("Thème : ");

        labelInclusion.setText("Partie de l'article : ");

        labelTitre.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        labelTitre.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        labelTitre.setText("Titre de l'article");

        btnRetourListeTree.setText("Retour à l'arborescence");
        btnRetourListeTree.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRetourListeTreeActionPerformed(evt);
            }
        });

        btnArchiverArticle.setText("Archiver tout l'article");
        btnArchiverArticle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnArchiverArticleActionPerformed(evt);
            }
        });

        labelContact.setText("E-mail rédacteur en chef : ");

        textFieldContenu.setColumns(20);
        textFieldContenu.setRows(5);
        jScrollPane1.setViewportView(textFieldContenu);

        labelErrorContent.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        labelErrorContent.setForeground(new java.awt.Color(153, 0, 0));
        labelErrorContent.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        labelErrorContent.setText("jLabel1");

        labelValidContent.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        labelValidContent.setForeground(new java.awt.Color(0, 153, 51));
        labelValidContent.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        labelValidContent.setText("jLabel1");

        btnSaveArticle.setText("Sauvegarder");
        btnSaveArticle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveArticleActionPerformed(evt);
            }
        });

        labelChefRedac.setText("Chef de redaction : ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(labelChefRedac)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelErrorContent, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jScrollPane1)
                            .addComponent(labelTitre, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(labelValidContent, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(labelTheme)
                                    .addComponent(labelInclusion)
                                    .addComponent(labelContact)
                                    .addComponent(labelTitreEcran))
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnRetourListeTree)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnArchiverArticle, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnSaveArticle))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(labelAuteur)
                                .addGap(0, 0, Short.MAX_VALUE)))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelTitreEcran)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelAuteur)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(labelChefRedac)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(labelContact)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(labelTheme)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(labelInclusion)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelTitre, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 57, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(labelValidContent)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(labelErrorContent)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnRetourListeTree)
                    .addComponent(btnArchiverArticle)
                    .addComponent(btnSaveArticle))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnRetourListeTreeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRetourListeTreeActionPerformed
        MainController mainController = MainController.getInstance();
        mainController.setCurrentView(new TreeDocumentsView());
        mainController.setCurrentPartieEdition(null);   
    }//GEN-LAST:event_btnRetourListeTreeActionPerformed

    private void btnArchiverArticleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnArchiverArticleActionPerformed
        //Lancement de l'archivage de tout l'article
        MainController mainController = MainController.getInstance();
        PartieRedaction partieEdition = mainController.getCurrentPartieEdition();
        Auteur auteurCourant = mainController.getCurrentAuteur();
        try {
            PartieRedactionDAL.archivageArticleRedaction(partieEdition.getIdPartie(), auteurCourant.getIdAuteur());
            this.catchValid("L'article a bien été archivé");            
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            this.catchError(e.getErrorCode());
        }
    }//GEN-LAST:event_btnArchiverArticleActionPerformed

    private void btnSaveArticleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveArticleActionPerformed
        // Sauvegarde des modifications sur l'article*
        MainController mainController = MainController.getInstance();
        PartieRedaction currentPartie = mainController.getCurrentPartieEdition();
        currentPartie.setContenuPartie(textFieldContenu.getText());
        
        // Sauvegarde de la partie dans la base
        PartieRedactionDAL.updatePartieRedaction(currentPartie);
        
        // Info retour
        this.catchValid("La partie a bien été sauvegardée !");
    }//GEN-LAST:event_btnSaveArticleActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnArchiverArticle;
    private javax.swing.JButton btnRetourListeTree;
    private javax.swing.JButton btnSaveArticle;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel labelAuteur;
    private javax.swing.JLabel labelChefRedac;
    private javax.swing.JLabel labelContact;
    private javax.swing.JLabel labelErrorContent;
    private javax.swing.JLabel labelInclusion;
    private javax.swing.JLabel labelTheme;
    private javax.swing.JLabel labelTitre;
    private javax.swing.JLabel labelTitreEcran;
    private javax.swing.JLabel labelValidContent;
    private javax.swing.JTextArea textFieldContenu;
    // End of variables declaration//GEN-END:variables

    @Override
    public String getTitle() {
        return title;
    }

    protected void catchError(int errorCode) {
        // Recupération de l'erreur
        OracleExceptionManager exceptionManager = OracleExceptionManager.getInstance();
        String errorToDisplay = exceptionManager.getMessage(errorCode);

        // Envoi de l'erreur au label
        this.labelErrorContent.setText(errorToDisplay);
        this.labelErrorContent.setVisible(true);
        this.labelValidContent.setVisible(false);
    }

    protected void catchValid(String validToDisplay) {
        // Envoi de l'erreur au label
        this.labelValidContent.setText(validToDisplay);
        this.labelValidContent.setVisible(true);
        this.labelErrorContent.setVisible(false);
    }
}
